import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uikit/utils/screen_util/size_extension.dart';

import '../../widgets/app_bar_widget.dart';
import '../../widgets/color.dart';
import '../../widgets/login_button.dart';
import '../../widgets/login_effect.dart';
import '../view_model/login_base_view_model.dart';

abstract class LoginBaseState<T extends StatefulWidget, A extends LoginBaseViewModel> extends State<T> {
  String get appBarTitle;

  String get rightTitle;

  String get bottomTitle;

  List<Widget> buildContentChildren(BuildContext context);

  /// 检查参数
  void checkParams();

  void pushNextPage();

  @override
  Widget build(BuildContext context) {
    return Theme(
      data: loginThemeData,
      child: Scaffold(
        appBar: AppBarWidget.normal(
            title: appBarTitle,
            rightTitle: rightTitle,
            rightButtonClick: () {
              pushNextPage();
            }),
        body: buildProviderWidget(),
      ),
    );
  }

  Widget buildProviderWidget();

  Widget buildBodyWidget() {
    return SingleChildScrollView(
      child: Column(
        children: [
          buildLoginEffect(),
          ...buildContentChildren(context),
          buildBottomLogin(),
        ],
      ),
    );
  }

  /// 顶部动画
  Widget buildLoginEffect() {
    return Selector<A, bool>(
      selector: (cxt, vm) {
        return vm.isProtected;
      },
      shouldRebuild: (pre, next) {
        return pre != next;
      },
      builder: (cxt, value, child) {
        print("_buildLoginEffect builder");
        return LoginEffect(protect: value);
      },
    );
  }

  /// 底部登录
  Widget buildBottomLogin() {
    return Selector<A, bool>(
      selector: (cxt, vm) {
        return vm.loginEnable;
      },
      shouldRebuild: (pre, next) {
        return pre != next;
      },
      builder: (cxt, value, child) {
        print("_buildBottomLogin builder $value");
        return Padding(
          padding: EdgeInsets.only(top: 20.w, left: 20.w, right: 20.w),
          child: LoginButton(bottomTitle, enable: value, onPressed: checkParams),
        );
      },
    );
  }
}
